package models

import (
	"time"
)

// PlatformSwitchLog 平台切换日志模型
type PlatformSwitchLog struct {
	BaseModel
	UserID                int64   `json:"user_id" gorm:"not null;index:idx_user_id"`
	OldPlatform           string  `json:"old_platform" gorm:"type:varchar(32);not null;index:idx_old_platform"`
	NewPlatform           string  `json:"new_platform" gorm:"type:varchar(32);not null;index:idx_new_platform"`
	SwitchReason          *string `json:"switch_reason" gorm:"type:varchar(255)"`
	OperatorID            *int64  `json:"operator_id" gorm:"index:idx_operator_id"`
	ConstraintCheckResult string  `json:"constraint_check_result" gorm:"type:json"`
	Status                string  `json:"status" gorm:"type:varchar(20);default:success;not null;index:idx_status"`
	ErrorMessage          *string `json:"error_message" gorm:"type:text"`
	User                  User    `gorm:"foreignKey:UserID" json:"user"`
	Operator              *User   `gorm:"foreignKey:OperatorID" json:"operator"`
}

// TableName 获取表名
func (PlatformSwitchLog) TableName() string {
	return "platform_switch_logs"
}

// ConstraintCheckResult 约束检查结果
type ConstraintCheckResult struct {
	HasCards          bool   `json:"has_cards"`
	HasAdminTransfers bool   `json:"has_admin_transfers"`
	HasSubAccounts    bool   `json:"has_sub_accounts"`
	HasMasterAccount  bool   `json:"has_master_account"`
	HasChildAccounts  bool   `json:"has_child_accounts"`
	CanSwitch         bool   `json:"can_switch"`
	Reason            string `json:"reason,omitempty"`
}

// SwitchConstraint 切换约束信息
type SwitchConstraint struct {
	HasCards          bool   `json:"has_cards"`
	HasAdminTransfers bool   `json:"has_admin_transfers"`
	HasSubAccounts    bool   `json:"has_sub_accounts"`
	HasMasterAccount  bool   `json:"has_master_account"`
	HasChildAccounts  bool   `json:"has_child_accounts"`
	CanSwitch         bool   `json:"can_switch"`
	Reason            string `json:"reason,omitempty"`
}

// PlatformSwitchRequest 平台切换请求
type PlatformSwitchRequest struct {
	TargetPlatform string `json:"target_platform" binding:"required,oneof=photonpay hlpay"`
	Confirm        bool   `json:"confirm" binding:"required"`
	Reason         string `json:"reason"`
}

// PlatformSwitchResponse 平台切换响应
type PlatformSwitchResponse struct {
	OldPlatform string    `json:"old_platform"`
	NewPlatform string    `json:"new_platform"`
	SwitchedAt  time.Time `json:"switched_at"`
}

// PlatformSwitchCheckResponse 平台切换检查响应
type PlatformSwitchCheckResponse struct {
	CanSwitch   bool             `json:"can_switch"`
	Constraints SwitchConstraint `json:"constraints"`
	Reason      *string          `json:"reason,omitempty"`
}

// SupportedPlatforms 支持的平台列表
var SupportedPlatforms = []string{"photonpay", "hlpay"}

// IsSupportedPlatform 检查平台是否支持
func IsSupportedPlatform(platform string) bool {
	for _, supported := range SupportedPlatforms {
		if supported == platform {
			return true
		}
	}
	return false
}
